//
//  WDMineHeaderView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/16.
//

#import "WDMineHeaderView.h"
#import "WDMineViewModel.h"

@interface WDMineHeaderView ()

@property (nonatomic, strong) WDMineViewModel *viewModel;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIImageView *headerImgView;


/// 昵称
@property (nonatomic, strong) UILabel *nickNameLabel;

/// 职务
@property (nonatomic, strong) UILabel *postNameLabel;

/// 电话
@property (nonatomic, strong) UILabel *phoneLabel;

/// 公司名
@property (nonatomic, strong) UILabel *companyLabel;


/// 头像
- (void)initWithHeaderImgView;

/// 昵称
- (void)initWithNickNameLabel;

/// 职务
- (void)initWithPostNameLabel;

/// 电话
- (void)initWithPhoneLabel;

/// 公司名
- (void)initWithCompanyLabel;


@end

@implementation WDMineHeaderView

- (void)initWithSubView {
    [super initWithSubView];
    
    self.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    
    [self initWithBgView];
}

- (void)initWithBgView {
    if (!_bgView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kWDMineHeaderViewHeight - 10);
        _bgView = [[UIView alloc] initWithFrame:frame];
        [self addSubview:_bgView];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
}

/// 头像
- (void)initWithHeaderImgView {
    if (!_headerImgView) {
        CGRect frame = CGRectMake(20, 0, 80, 80);
        _headerImgView = [[UIImageView alloc] initWithFrame:frame];
//        _headerImgView.backgroundColor = [UIColor orangeColor];
        _headerImgView.layer.cornerRadius = frame.size.height * 0.5;
        _headerImgView.layer.masksToBounds = YES;
        _headerImgView.userInteractionEnabled = YES;
        [self.bgView addSubview:_headerImgView];
        
        @weakify(self);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            @strongify(self);
            [self actionWithHeaderImgView];
        }];
        [self.headerImgView addGestureRecognizer:tap];
    }
    
    if ([EDSAppDelegateTool sharedInstance].userInfoModel) {
        [_headerImgView sd_setImageWithURL:[NSURL URLWithString:[EDSAppDelegateTool sharedInstance].userInfoModel.avatar] placeholderImage:[UIImage imageNamed:@"ic_default_head_portrait"]];
    } else {
//        _companyLabel.text = @"";
    }
}

/// 昵称
- (void)initWithNickNameLabel {
    if (!_nickNameLabel) {
        NSString *str = @"逍遥游";
        UIFont *font = [UIFont systemFontOfSize:20 weight:UIFontWeightSemibold];
        float width = [str widthForFont:font] + 2;
        CGRect frame = CGRectMake(self.headerImgView.right + 12, self.headerImgView.top, width, 28);
        _nickNameLabel = [UILabel createWithLabel:self.bgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor blackColor];
//            label.text = str;
        }];
    }
    self.nickNameLabel.text = @"";
    if ([EDSAppDelegateTool sharedInstance].userInfoModel) {
        NSString *str = objectNull([EDSAppDelegateTool sharedInstance].userInfoModel.realName);
        float width = [str widthForFont:self.nickNameLabel.font] + 2;
        self.nickNameLabel.width = width;
        self.nickNameLabel.text = str;
    }
}

/// 职务
- (void)initWithPostNameLabel {
    if (!_postNameLabel) {
        NSString *str = @"入库质检";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        float width = [str widthForFont:font] + 6;
        CGRect frame = CGRectMake(self.nickNameLabel.right + 6, 0, width, 18);
        _postNameLabel = [UILabel createWithLabel:self.bgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#ffffff"];
            label.backgroundColor = [UIColor colorWithHexString:@"#19C46E"];
//            label.text = str;
            label.textAlignment = NSTextAlignmentCenter;
            label.layer.cornerRadius = 2;
            label.layer.masksToBounds = YES;
        }];
    }
    _postNameLabel.centerY = self.nickNameLabel.centerY;
    _postNameLabel.left = self.nickNameLabel.right + 10;
    if ([EDSAppDelegateTool sharedInstance].userInfoModel) {
        NSString *postName = objectNull([EDSAppDelegateTool sharedInstance].userInfoModel.roleName);
        postName = [postName stringByReplacingOccurrencesOfString:@"服务站-" withString:@""]; // 去掉“服务站”几个字
        postName = [postName stringByReplacingOccurrencesOfString:@"服务站" withString:@""];
//        NSString *str = objectNull([EDSAppDelegateTool sharedInstance].userInfoModel.roleName);
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        float width = [postName widthForFont:font] + 6;
        _postNameLabel.width = width;
        _postNameLabel.text = postName;
    } else {
        _postNameLabel.text = @"";
    }
}

/// 电话
- (void)initWithPhoneLabel {
    if (!_phoneLabel) {
        NSString *str = @"18261929604567";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        float width = [str widthForFont:font] + 2;
        CGRect frame = CGRectMake(self.nickNameLabel.left, 0, width, 18);
        _phoneLabel = [UILabel createWithLabel:self.bgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
//            label.text = str;
        }];
    }
    self.phoneLabel.centerY = self.headerImgView.centerY;
    if ([EDSAppDelegateTool sharedInstance].userInfoModel) {
        _phoneLabel.text = objectNull([EDSAppDelegateTool sharedInstance].userInfoModel.phone);
    } else {
        _phoneLabel.text = @"";
    }
}

/// 公司名
- (void)initWithCompanyLabel {
    if (!_companyLabel) {
        CGRect frame = CGRectMake(self.nickNameLabel.left, self.headerImgView.bottom - 18, self.width - self.nickNameLabel.left - 20, 18);
        _companyLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
//            label.text = @"XXXXXXXXXXX服务站";
        }];
    }
    if ([EDSAppDelegateTool sharedInstance].userInfoModel) {
        _companyLabel.text = objectNull([EDSAppDelegateTool sharedInstance].userInfoModel.stationName);
    } else {
        _companyLabel.text = @"";
    }
}

/// 点击头像
- (void)actionWithHeaderImgView {
    if (self.touchHeaderImageViewBlock) {
        self.touchHeaderImageViewBlock();
    }
}


- (void)updateWithViewData:(id)data {
    
    self.viewModel = data;
    
    /// 头像
    [self initWithHeaderImgView];

    /// 昵称
    [self initWithNickNameLabel];

    /// 职务
    [self initWithPostNameLabel];

    /// 电话
    [self initWithPhoneLabel];

    /// 公司名
    [self initWithCompanyLabel];
}


@end
